//
//  ZZVipPayController.m
//  QQplayground
//
//  Created by zhangxy on 2018/8/16.
//  Copyright © 2018年 zhang. All rights reserved.
//
#import "ZZVipPayController.h"

#import "ZZPayView.h"


@interface ZZVipPayController ()

@property(nonatomic,strong) UIScrollView *scrollView;

@end

@implementation ZZVipPayController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"Q友会员";
    [self setNavigationBar];
    self.navigationController.navigationBarHidden = NO;
    [self.view setBackgroundColor:UIColorFromRGB(BgSystemColor)];
    
    //表情盘
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, NavBarHeight, ScreenWidth, ScreenHeight-NavBarHeight - 112)];
    _scrollView.pagingEnabled = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    [_scrollView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin];
    [_scrollView setAutoresizesSubviews:YES];
    //添加键盘View
    [self.view addSubview:_scrollView];
    [self createBottomView];
    
    int i=0;
    for(NSArray *items in _listArray){
        for (NSDictionary *item in items) {
            [self createItemView:item];
            i = i+1;
        }
    }
    
    [_scrollView setContentSize:CGSizeMake(ScreenWidth * i, ScreenHeight-NavBarHeight - 112)];
    [_scrollView setContentOffset:CGPointMake((_code - 1) * ScreenWidth, 0) animated:YES];
    

//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(WXPayResult:) name:@"WXPayResult" object:nil];
}

//-(void)WXPayResult:(NSNotification *) info{
//    NSDictionary *dict = info.object;
//    [self checkResultStatus:dict];
//}

//-(void)checkResultStatus:(NSDictionary *) dict{
//    NSLog(@"dict:-------------%@", dict);
//    if([dict[@"code"] intValue] == 0 && isShowPayResult){
//        isShowPayResult = NO;
//
//        [self backAction];
//
//        if(_VipPayResult){
//            _VipPayResult(0,dict[@"message"]);
//        }
//    }else{
//        [PopUtil showMessage:dict[@"message"]];
//    }
//}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}


//返回订单号，使用回调让上一级去处理，本级只负责调起微信支付和传递数据
-(void)openVip:(UIButton *) btn{
    NSLog(@"开通VIP ------------ ");
    ZZPayView *pay = [[ZZPayView alloc] initWithShareType:self result:^(int code, NSDictionary *msg) {
        _VipPayResult(msg);
        [self backAction];
    }];
    [pay show];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void) createItemView:(NSDictionary *) dict{
    
    int icode = [dict[@"code"] intValue];
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake((icode-1)*ScreenWidth, 0, ScreenWidth, _scrollView.frame.size.height)];
    [header setBackgroundColor:UIColor.clearColor];
    
    UIImageView *imgAvatar = [[UIImageView  alloc] initWithFrame:CGRectMake(0, 92, ScreenWidth, header.size.height - 92)];
    [imgAvatar setBackgroundColor:UIColor.whiteColor];
    imgAvatar.layer.cornerRadius = 32.5f;
    imgAvatar.layer.masksToBounds = YES;
    [imgAvatar setContentMode:UIViewContentModeScaleAspectFit];
    [imgAvatar setImage:[UIImage imageNamed:[NSString stringWithFormat:@"bg_vip%d",icode]]];
    
    [header addSubview:imgAvatar];
    
    
    UILabel *labName = [[UILabel alloc] initWithFrame:CGRectMake(20, 15, ScreenWidth - 40, 21)];
    [labName setFont:FontFiftent];
    [labName setText:dict[@"title"]];
    [labName setTextColor:UIColorFromRGB(TextSizeOneColor)];
    [header addSubview:labName];
    
    
    
    UILabel *_labVipValidate = [self createLabel];
    [_labVipValidate setFrame:CGRectMake(20,42, ScreenWidth - 40, 21)];
    [_labVipValidate setFont:FontFiftent];
    [_labVipValidate setTextColor:UIColorFromRGB(TextSizeEightColor)];
    [_labVipValidate setText:dict[@"desc"]];
    [header addSubview:_labVipValidate];
    
    
    [_scrollView addSubview:header];
}


-(void) createBottomView{
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight - 112 - NavBarHeight, ScreenWidth, 112)];
    
    UILabel *labName = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, ScreenWidth-40, 21)];
    [labName setFont:FontThirteen];
    [labName setText:@"此功能需Q友乐园3.0及以上版本支持"];
    [labName setTextAlignment:NSTextAlignmentLeft];
    [labName setTextColor:UIColorFromRGB(TextSizeEightColor)];
    [header addSubview:labName];
    
    UIButton *btnOpen = [self createButton];
    [btnOpen setFrame:CGRectMake(10,58 ,ScreenWidth - 20 , 44)];
    if([UserUtil getUser].is_vip){
        [btnOpen setTitle:@"续费会员" forState:0];
    }else{
        [btnOpen setTitle:@"开通会员" forState:0];
    }
    [btnOpen setBackgroundColor:UIColorFromRGB(BgTitleColor)];
    btnOpen.layer.cornerRadius = 6.0f;
    btnOpen.layer.masksToBounds = YES;
    [btnOpen addTarget:self action:@selector(openVip:) forControlEvents:UIControlEventTouchUpInside];
    [header addSubview:btnOpen];
    
    [self.view addSubview:header];
}



- (CGSize )autoWidthOfLabel:(UILabel *)label with:(CGFloat )width{
    //Calculate the expected size based on the font and linebreak mode of your label
    // FLT_MAX here simply means no constraint in height
    CGSize maximumLabelSize = CGSizeMake(FLT_MAX, width);
    
    CGSize expectedLabelSize = [label sizeThatFits:maximumLabelSize];
    
    //adjust the label the the new height.
    CGRect newFrame = label.frame;
    newFrame.size.width = expectedLabelSize.width;
    label.frame = newFrame;
    [label updateConstraintsIfNeeded];
    
    return expectedLabelSize;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
